package com.softer.chengxin.dicinfo.dao;

import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.dicinfo.dao.provider.DicInfoDaoProvider;
import com.softer.chengxin.dicinfo.entity.DicInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zcz
 * @date 2018/12/20
 */
@Mapper
@Repository
public interface DicInfoDao {
    /**
     * 加载字典信息，根据字典类型code
     * @param page      页面信息
     * @return 字典信息列表
     */
    @SelectProvider(type = DicInfoDaoProvider.class, method = "getList")
    List<DicInfo> getList(Page page);
    /**
     * 查询count
     * @return count
     */
    @Select("SELECT COUNT(`id`) FROM dic_info WHERE `code` LIKE CONCAT(#{typeCode},'%')")
    int getCount(String typeCode);

    /**
     * 新增
     * @param dicInfo 字典信息
     * @return 处理结果
     */
    @Insert("INSERT INTO dic_info(`name`,`code`,`type`,adder,add_time,description) VALUES(#{name},#{code},#{type},#{adder},now(),#{description})")
    int insert(DicInfo dicInfo);

    /**
     * 修改字典类型
     * @param dicInfo 角色信息
     * @return 处理结果
     */
    @UpdateProvider(type = DicInfoDaoProvider.class, method = "update")
    int update(DicInfo dicInfo);

    /**
     * 查询最大的code
     * @return
     */
    @Select("SELECT MAX(`code`) FROM dic_info WHERE `code` LIKE CONCAT(#{code},'%')")
    String getMaxCode(String code);

    /**
     * 删除字典信息
     * @param id
     * @return
     */
    @Delete("DELETE FROM dic_info WHERE id = #{id}")
    int delete(String id);

    /**
     * 根据id查询字典信息表
     * @param id
     * @return
     */
    @Select("SELECT id,`code`,`name`,`adder`,DATE_FORMAT(add_time, '%Y-%m-%d') AS `addTime`,description FROM dic_info WHERE id = #{id}")
    DicInfo getDicInfoById(String id);

    /**
     * 根据字典类型查询字典信息列表
     * @param typeCode
     * @return
     */
    @Select("SELECT `name`,`code` FROM dic_info WHERE `code` LIKE CONCAT(#{typeCode},'%')")
    List<DicInfo> getDicInfoByTypeCode(String typeCode);
}
